<?php
/**
 * 同城微聚汇
 * ============================================================================
 * * 版权所有 2015-2027 武汉鼎联信息技术有限公司，并保留所有权利。
 * 网站地址: http://dlshop.dliansoft.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: 当燃 2016-01-09
 */
namespace app\mobile\controller;
use app\home\logic\UsersLogic;
use Think\Db;
class Index extends MobileBase {

    public function index(){

        $type=I('type','index');

        /*首页商品*/
       $hot_good = M('goods')->where("is_real =1 and is_hot =1 and is_on_sale=1 and on_time<=".time()." and off_time>=".time()."")->order('sort desc,goods_id desc')->limit(6)->cache(true,TPSHOP_CACHE_TIME)->select();
       $recommend_good = M('goods')->where("is_real =1 and is_recommend=1 and is_on_sale=1 and on_time<=".time()." and off_time>=".time()."")->order('sort desc,goods_id desc')->limit(6)->cache(true,TPSHOP_CACHE_TIME)->select();
       $new_good = M('goods')->where("is_real =1 and is_new=1 and is_on_sale=1 and on_time<=".time()." and off_time>=".time()."")->order('sort desc,goods_id desc')->limit(10)->cache(true,TPSHOP_CACHE_TIME)->select();

        $condi['start_time']=array('elt',time());
        $condi['end_time']=array('egt',time());

        $proms=M('prom_goods')->where($condi)->select();
        //筛选符合条件的促销商品
        foreach ($proms as $k => $val) {
             if(in_array($user['level'],explode(',', $val['group']))){
                $promss[$val['id']]=$val;
             }
         }
         $flash=M('flash_sale')->where($condi)->select();
         foreach ($flash as $k => $val) {
            $flasharr[$val['id']]=$val;
         }

         $group=M('group_buy')->where($condi)->select();
         foreach ($group as $k => $val) {
            $grouparr[$val['id']]=$val;
         }



        

        if($recommend_good){
            foreach ($recommend_good as $key => $value) {
                //如果是限时抢购商品
                if($value['prom_type']==1){
                    $resarr=$flasharr;
                    if($resarr[$value['prom_id']]['price']){
                        $value['shop_price']=$resarr[$value['prom_id']]['price'];
                        $value['is_prom']=1;
                    }
                }
                //如果是团购商品
                
                if($value['prom_type']==2){
                    $resarr=$grouparr;
                    if($resarr[$value['prom_id']]['price']){
                        $value['shop_price']=$resarr[$value['prom_id']]['price'];
                        $value['is_prom']=2;
                    }
                }
                //如果是促销商品
                if($value['prom_type']==3){
                    $resarr=$promss;
                    if(!empty($promss[$value['prom_id']])){
                        $prominfo=$promss[$value['prom_id']];
                        if($prominfo['type'] == 0){
                            $value['shop_price'] = $value['shop_price']*$prominfo['expression']/100;//打折优惠
                        }elseif($prominfo['type'] == 1){
                            $value['shop_price'] = $value['shop_price']-$prominfo['expression'];//减价优惠
                        }elseif($prominfo['type']==2){
                            $value['shop_price'] = $prominfo['expression'];//固定金额优惠
                        }
                        $value['prom_name']=$prominfo['name'];
                        $value['is_prom']=3;
                    }
                }
                
                $value['start_time']=$resarr[$value['prom_id']]['start_time'];
                $value['end_time']=$resarr[$value['prom_id']]['end_time'];
                $recommend_goods[$key]=$value;
            }
        }


        if($hot_good){
            foreach ($hot_good as $key => $value) {
                //如果是限时抢购商品
                if($value['prom_type']==1){
                    $resarr=$flasharr;
                    if($resarr[$value['prom_id']]['price']){
                        $value['shop_price']=$resarr[$value['prom_id']]['price'];
                        $value['is_prom']=1;
                    }
                }
                //如果是团购商品
                if($value['prom_type']==2){
                    $resarr=$grouparr;
                    if($resarr[$value['prom_id']]['price']){
                        $value['shop_price']=$resarr[$value['prom_id']]['price'];
                        $value['is_prom']=2;
                    }
                }
                //如果是促销商品
                if($value['prom_type']==3){
                    $resarr=$promss;
                    if(!empty($promss[$value['prom_id']])){
                        $prominfo=$promss[$value['prom_id']];
                        if($prominfo['type'] == 0){
                            $value['shop_price'] = $value['shop_price']*$prominfo['expression']/100;//打折优惠
                        }elseif($prominfo['type'] == 1){
                            $value['shop_price'] = $value['shop_price']-$prominfo['expression'];//减价优惠
                        }elseif($prominfo['type']==2){
                            $value['shop_price'] = $prominfo['expression'];//固定金额优惠
                        }
                        $value['prom_name']=$prominfo['name'];
                        $value['is_prom']=3;
                    }
                }
                
                $value['start_time']=$resarr[$value['prom_id']]['start_time'];
                $value['end_time']=$resarr[$value['prom_id']]['end_time'];
                $hot_goods[$key]=$value;
            }
        }

        if($new_good){
            foreach ($new_good as $key => $value) {
                //如果是限时抢购商品
                if($value['prom_type']==1){
                    $resarr=$flasharr;
                    if($resarr[$value['prom_id']]['price']){
                        $value['shop_price']=$resarr[$value['prom_id']]['price'];
                        $value['is_prom']=1;
                    }
                }
                //如果是团购商品
                if($value['prom_type']==2){
                    $resarr=$grouparr;
                    if($resarr[$value['prom_id']]['price']){
                        $value['shop_price']=$resarr[$value['prom_id']]['price'];
                        $value['is_prom']=2;
                    }
                }
                //如果是促销商品
                if($value['prom_type']==3){
                    $resarr=$promss;
                    if(!empty($promss[$value['prom_id']])){
                        $prominfo=$promss[$value['prom_id']];
                        if($prominfo['type'] == 0){
                            $value['shop_price'] = $value['shop_price']*$prominfo['expression']/100;//打折优惠
                        }elseif($prominfo['type'] == 1){
                            $value['shop_price'] = $value['shop_price']-$prominfo['expression'];//减价优惠
                        }elseif($prominfo['type']==2){
                            $value['shop_price'] = $prominfo['expression'];//固定金额优惠
                        }
                        $value['prom_name']=$prominfo['name'];
                        $value['is_prom']=3;
                    }
                }
                
                $value['start_time']=$resarr[$value['prom_id']]['start_time'];
                $value['end_time']=$resarr[$value['prom_id']]['end_time'];
                $new_goods[$key]=$value;
            }
        }
       
             

        $this->assign('hot_goods',$hot_goods);
        $this->assign('recommend_goods',$recommend_goods);
        $this->assign('new_goods',$new_goods);

        $shareLink = "http://$_SERVER[HTTP_HOST]".$_SERVER["REQUEST_URI"];
        $this->assign('shareLink',$shareLink);
        $this->assign('time',time());
        $this->assign('type',$type);
        return $this->fetch('index3');
    }


    public function shoplist(){
        if(IS_AJAX){
                $local=I('post.nowlocat');//当前位置坐标
                $arr1=explode(",", $local);
                $lat1=(float)$arr1[0];
                $lng1=(float)$arr1[1];
                $pickupList = DB::name('pick_up')
                        ->alias('p')
                        ->field(
                            'p.*,r1.name as province_name,
                            r2.name as city_name,
                            r3.name as district_name,
                            s.suppliers_name,
                            (    
                                6371 * acos (    
                                  cos ( radians('.$lat1.') )    
                                  * cos( radians( p.lat ) )    
                                  * cos( radians( p.lng ) - radians('.$lng1.') )    
                                  + sin ( radians('.$lat1.') )    
                                  * sin( radians( p.lat ) )    
                                )    
                              ) as distance')
                        ->join('__REGION__ r1','r1.id = p.province_id','LEFT')
                        ->join('__REGION__ r2','r2.id = p.city_id','LEFT')
                        ->join('__REGION__ r3','r3.id = p.district_id','LEFT')
                        ->join('__SUPPLIERS__ s','s.suppliers_id = p.suppliersid','LEFT')
                        ->having('distance<600')
                        ->order('distance')
                        ->select();
                $htmlstr="";
                foreach ($pickupList as $key => $value) {

                    $htmlstr.='<a href="'.U("index/shopinfo").'?id='.$value[pickup_id].'">
                                <div class="weui-cell">
                                    <div class="weui-cell__hd"><img src="'.$value[pickup_logo].'" alt="" style="width:35px;margin-right:5px;display:block;vertical-align:top"></div>
                                    <div class="weui-cell__bd" style="padding-right:15px">
                                        <p class="bt">'.$value[pickup_name].'</p>
                                        <p class="bt2">'.$value[province_name].$value[city_name].$value[district_name].$value[pickup_address].'</p>
                                    </div>
                                    <div class="weui-cell__ft" style="font-size:13px">'.mToKm($value[distance]).'</div>
                                </div></a>';
                }
                echo $htmlstr;
                
            }else{
                return $this->fetch();
            }
  
        
    }

    public function shopinfo(){
        $pickup_id=I('get.id');
        $shopinfo = DB::name('pick_up')
                ->alias('p')
                ->field('p.*,r1.name as province_name,r2.name as city_name,r3.name as district_name,s.suppliers_name')
                ->join('__REGION__ r1','r1.id = p.province_id','LEFT')
                ->join('__REGION__ r2','r2.id = p.city_id','LEFT')
                ->join('__REGION__ r3','r3.id = p.district_id','LEFT')
                ->join('__SUPPLIERS__ s','s.suppliers_id = p.suppliersid','LEFT')
                ->where('p.pickup_id',$pickup_id)
                ->find();
        $imgs=substr($shopinfo['pickup_images'],0,strlen($shopinfo['pickup_images'])-1);
        $pickup_images=explode(',',$imgs );
        $imgs=str_replace("/public", "http://".$_SERVER['HTTP_HOST']."/public", $imgs);
        $imgstr=str_replace(",", "','", $imgs);
        $this->assign('shopinfo',$shopinfo);
        $this->assign('pickup_images',$pickup_images);
        $this->assign('imgstr',$imgstr);
        return $this->fetch();
    }

    /**
     * 分类列表显示
     */
    public function categoryList(){
        return $this->fetch();
    }

     public function error_msg(){
        return $this->fetch();
    }

    /**
     * 模板列表
     */
    public function mobanlist(){
        $arr = glob("D:/wamp/www/svn_tpshop/mobile--html/*.html");
        foreach($arr as $key => $val)
        {
            $html = end(explode('/', $val));
            echo "<a href='http://www.php.com/svn_tpshop/mobile--html/{$html}' target='_blank'>{$html}</a> <br/>";            
        }        
    }
    
    /**
     * 商品列表页
     */
    public function goodsList(){
        $id = I('get.id/d',0); // 当前分类id
        $lists = getCatGrandson($id);
        $this->assign('lists',$lists);
        return $this->fetch();
    }
    
     public function ajaxGetMore(){
        $p = I('p/d',1);
        $type=I('type','index');

        //如果首页加载更多
        if($type=="index"){
            $moregood = M('goods')->where("is_real =1 and is_hot=0 and is_recommend = 0 and is_new=0 and is_on_sale=1 and on_time<=".time()." and off_time>=".time()."")->order('sort desc,goods_id desc')->page($p,8)->select();
        }
        if($type=="indexwoman"){
            $moregood = M('goods t0')->join('tp_goods_category t1','t0.cat_id=t1.id','left')->field('t0.*,t1.parent_id_path')->where("t0.is_real =1 and t0.is_on_sale=1 and t0.on_time<=".time()." and t0.off_time>=".time()." and t1.parent_id_path like '%_39_%'")->order('t0.sort desc,t0.goods_id desc')->page($p,8)->select();
        }

        if($type=="indexman"){
            $moregood = M('goods t0')->join('tp_goods_category t1','t0.cat_id=t1.id','left')->field('t0.*,t1.parent_id_path')->where("t0.is_real =1 and t0.is_on_sale=1 and t0.on_time<=".time()." and t0.off_time>=".time()." and t1.parent_id_path like '%_40_%'")->order('t0.sort desc,t0.goods_id desc')->page($p,8)->select();
        }
        if($type=="shopindex"){
            $select_goods=$this->weixin_config['select_goods'];
            $moregood = M('goods')->where("is_real =1 and is_on_sale=1 and on_time<=".time()." and off_time>=".time()."")->where("goods_id","in",$select_goods)->order('sort desc,goods_id desc')->page($p,8)->select();
        }

    
         $condi['start_time']=array('elt',time());
         $condi['end_time']=array('egt',time());

         $proms=M('prom_goods')->where($condi)->select();
         //筛选符合条件的促销商品
         foreach ($proms as $k => $val) {
              if(in_array($user['level'],explode(',', $val['group']))){
                 $promss[$val['id']]=$val;
              }
          }
          $flash=M('flash_sale')->where($condi)->select();
          foreach ($flash as $k => $val) {
             $flasharr[$val['id']]=$val;
          }

          $group=M('group_buy')->where($condi)->select();
          foreach ($group as $k => $val) {
             $grouparr[$val['id']]=$val;
          }


         if($moregood){
             foreach ($moregood as $key => $value) {
                 //如果是限时抢购商品
                 if($value['prom_type']==1){
                     $resarr=$flasharr;
                     if($resarr[$value['prom_id']]['price']){
                         $value['shop_price']=$resarr[$value['prom_id']]['price'];
                         $value['is_prom']=1;
                     }
                 }
                 //如果是团购商品
                 
                 if($value['prom_type']==2){
                     $resarr=$grouparr;
                     if($resarr[$value['prom_id']]['price']){
                         $value['shop_price']=$resarr[$value['prom_id']]['price'];
                         $value['is_prom']=2;
                     }
                 }
                 //如果是促销商品
                 if($value['prom_type']==3){
                     $resarr=$promss;
                     if(!empty($promss[$value['prom_id']])){
                         $prominfo=$promss[$value['prom_id']];
                         if($prominfo['type'] == 0){
                             $value['shop_price'] = $value['shop_price']*$prominfo['expression']/100;//打折优惠
                         }elseif($prominfo['type'] == 1){
                             $value['shop_price'] = $value['shop_price']-$prominfo['expression'];//减价优惠
                         }elseif($prominfo['type']==2){
                             $value['shop_price'] = $prominfo['expression'];//固定金额优惠
                         }
                         $value['prom_name']=$prominfo['name'];
                         $value['is_prom']=3;
                     }
                 }
                 
                 $value['start_time']=$resarr[$value['prom_id']]['start_time'];
                 $value['end_time']=$resarr[$value['prom_id']]['end_time'];
                 $moregoods[$key]=$value;
             }
         }


        $this->assign('moregoods',$moregoods);
        $this->assign('time',time());
        return $this->fetch('ajaxGetMore2');
    }
    public function getlocation(){
        $shoplist=M('pick_up')->where('is_update',0)->select();
        $this->assign('shoplist',$shoplist);
        return $this->fetch();
    }

}